//
//  SelectCityController.m
//  FastDoctor
//
//  Created by 程建良 on 16/7/7.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "SelectCityController.h"
#import "ProVincesModel.h"

@interface SelectCityController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic,strong)NSArray<ProVincesModel*>*listArray;
@property (nonatomic,strong)UIPickerView *pickerView;
@property (nonatomic,assign)NSInteger index;
@end

@implementation SelectCityController
-(NSArray<ProVincesModel *> *)listArray{
    if (!_listArray) {
        _listArray = [ProVincesModel listArray];
    }
    return _listArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.pickerView = [[UIPickerView alloc]initWithFrame:self.view.bounds];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:self.pickerView];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.listArray.count;
    }else{
        ProVincesModel *model = self.listArray[_index];
        return model.cities.count;
    }
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        ProVincesModel *model = self.listArray[row];
        return model.name;
    }else{
        ProVincesModel *model = self.listArray[_index];
        return model.cities[row];
    }
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        _index = row;
        [pickerView reloadComponent:1];
        if (!_index == 0) {
            ProVincesModel *model = self.listArray[_index];
            NSString *str = [NSString stringWithFormat:@"%@ 省/市 %@ 市/区",model.name,model.cities[0]];
            [[NSUserDefaults standardUserDefaults]setObject:str forKey:@"cicty"];
        }
    }else{
        if (!_index == 0) {
            ProVincesModel *model = self.listArray[_index];
            NSString *str = [NSString stringWithFormat:@"%@ 省/市 %@ 市/区",model.name,model.cities[row]];
            [[NSUserDefaults standardUserDefaults]setObject:str forKey:@"cicty"];
        }
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.block();
}
@end
